What is final-form-arrays?
The final-form-arrays package is an extension for Final Form that provides utilities for managing arrays in forms. It simplifies the process of handling dynamic fields, such as adding, removing, and reordering items in an array within a form.
What are final-form-arrays's main functionalities?
Adding Items to an Array
This feature allows you to dynamically add items to an array within a form. The code sample demonstrates how to use the FieldArray component to manage a list of friends, with the ability to add new friends and remove existing ones.
const { FieldArray } = require('final-form-arrays');
<FieldArray name="friends">
{({ fields }) => (
<div>
{fields.map((name, index) => (
<div key={name}>
<label>Friend #{index + 1}</label>
<Field name={`${name}.firstName`} component="input" placeholder="First Name" />
<Field name={`${name}.lastName`} component="input" placeholder="Last Name" />
<span onClick={() => fields.remove(index)}>-</span>
</div>
))}
<button type="button" onClick={() => fields.push({})}>Add Friend</button>
</div>
)}
</FieldArray>
Removing Items from an Array
This feature allows you to remove items from an array within a form. The code sample shows how to use the FieldArray component to manage a list of friends, with the ability to remove a friend by clicking a button.
const { FieldArray } = require('final-form-arrays');
<FieldArray name="friends">
{({ fields }) => (
<div>
{fields.map((name, index) => (
<div key={name}>
<label>Friend #{index + 1}</label>
<Field name={`${name}.firstName`} component="input" placeholder="First Name" />
<Field name={`${name}.lastName`} component="input" placeholder="Last Name" />
<span onClick={() => fields.remove(index)}>-</span>
</div>
))}
</div>
)}
</FieldArray>
Reordering Items in an Array
This feature allows you to reorder items in an array within a form. The code sample demonstrates how to use the FieldArray component to manage a list of friends, with the ability to move a friend up or down in the list.
const { FieldArray } = require('final-form-arrays');
<FieldArray name="friends">
{({ fields }) => (
<div>
{fields.map((name, index) => (
<div key={name}>
<label>Friend #{index + 1}</label>
<Field name={`${name}.firstName`} component="input" placeholder="First Name" />
<Field name={`${name}.lastName`} component="input" placeholder="Last Name" />
<span onClick={() => fields.move(index, index - 1)}>↑</span>
<span onClick={() => fields.move(index, index + 1)}>↓</span>
</div>
))}
</div>
)}
</FieldArray>
Other packages similar to final-form-arrays
react-final-form-arrays
react-final-form-arrays is a similar package that provides array field management for React Final Form. It offers similar functionalities such as adding, removing, and reordering items in an array. The main difference is that it is specifically designed to work with React Final Form, whereas final-form-arrays can be used with any Final Form implementation.
formik
Formik is a popular form library for React that also provides utilities for managing arrays in forms. It offers similar functionalities such as dynamic field arrays, validation, and more. Formik is more comprehensive and provides a wider range of features beyond just array management, making it a more versatile choice for complex form handling.
react-hook-form
react-hook-form is another popular form library for React that provides efficient and flexible form handling, including array field management. It offers similar functionalities such as adding, removing, and reordering items in an array. react-hook-form is known for its performance and minimal re-renders, making it a good choice for performance-sensitive applications.